package Text27;
import java.util.*;
public class Main {
    public static int N = 310;
    public static int[] s = new int[N];//前缀和
    public static int[][] f = new int[N][N];//合并区间[i,j]的最小代价

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        for (int i = 1; i <=n ; i++) {
            s[i]=scanner.nextInt();
            s[i]+=s[i-1];
        }
        for (int len = 2; len <=n ; len++) {
            for (int l = 1; l+len-1<=n ; l++) {
                int r=l+len-1;
                f[l][r]=0x3f3f3f3f;
                for (int k = l; k <=r-1 ; k++) {
                    f[l][r]=Math.min(f[l][r],f[l][k]+f[k+1][r]+s[r]-s[l-1]);
                }
            }
        }
        System.out.println(f[1][n]);
    }
}
